package com.warom.sdg.webview.jsinterface;

import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.google.gson.Gson;
import com.warom.sdg.model.ApiResponse;
import com.warom.sdg.model.VentilationConfigEntity;
import com.warom.sdg.service.VentilationConfigService;
import com.warom.sdg.webview.BaseWebViewJsInterface;
import com.warom.sdg.webview.WebViewWrapper;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

public class WebViewJsVentilationConfigInterface extends BaseWebViewJsInterface {

    private static final String TAG = "VentilationConfigJsInterface";
    private final VentilationConfigService ventilationConfigService;
    private final ExecutorService executor = Executors.newSingleThreadExecutor();
    private final Gson gson = new Gson();

    public WebViewJsVentilationConfigInterface(Context context) {
        super(context, null);
        this.ventilationConfigService = new VentilationConfigService((Application) context.getApplicationContext());
    }

    /**
     * 构造函数，带WebViewWrapper参数
     * @param context 上下文
     * @param webViewWrapper WebView包装类，用于回调
     */
    public WebViewJsVentilationConfigInterface(Context context, WebViewWrapper webViewWrapper) {
        super(context, webViewWrapper);
        this.ventilationConfigService = new VentilationConfigService((Application) context.getApplicationContext());
    }

    /**
     * 异步获取通风配置列表.
     * @param args 空参数，为保持接口一致性.
     * @param requestId 请求ID.
     */
    @JavascriptInterface
    public void getVentilationConfigList(String args, String requestId) {
        executor.execute(() -> getConfigList(args, result -> handleAsyncResponse(requestId, result)));
    }

    private void getConfigList(String args, Consumer<String> callback) {
        try {
            ApiResponse<List<VentilationConfigEntity>> response = ventilationConfigService.getVentilationConfigList();
            callback.accept(gson.toJson(response));
        } catch (Exception e) {
            Log.e(TAG, "获取通风配置列表失败", e);
            ApiResponse<Object> errorResponse = new ApiResponse<>(false, e.getMessage(), null);
            callback.accept(gson.toJson(errorResponse));
        }
    }

    /**
     * 异步更新单条通风配置.
     * @param params JSON字符串格式的VentilationConfigEntity.
     * @param requestId 请求ID.
     */
    @JavascriptInterface
    public void updateVentilationConfig(String params, String requestId) {
        executor.execute(() -> updateConfig(params, result -> handleAsyncResponse(requestId, result)));
    }

    private void updateConfig(String params, Consumer<String> callback) {
        try {
            VentilationConfigEntity configEntity = gson.fromJson(params, VentilationConfigEntity.class);
            if (configEntity == null) {
                throw new IllegalArgumentException("无效的配置更新参数");
            }
            ApiResponse<VentilationConfigEntity> response = ventilationConfigService.updateVentilationConfig(configEntity);
            callback.accept(gson.toJson(response));
        } catch (Exception e) {
            Log.e(TAG, "更新通风配置失败", e);
            ApiResponse<Object> errorResponse = new ApiResponse<>(false, e.getMessage(), null);
            callback.accept(gson.toJson(errorResponse));
        }
    }
} 